import { getSalesData } from '../api/salesData.js';

document.addEventListener('DOMContentLoaded', async () => {
  const salesTableBody = document.querySelector('.sales-table tbody');
  const monthDisplay = document.querySelector('.month-display h2');
  const loadingIndicator = document.createElement('div');
  
  // 添加加载指示器
  loadingIndicator.className = 'loading';
  loadingIndicator.textContent = '加载中...';
  salesTableBody.parentElement.appendChild(loadingIndicator);

  try {
    // 获取数据
    const { currentMonth, salesData } = await getSalesData();
    
    // 更新月份显示
    monthDisplay.textContent = currentMonth;

    // 渲染表格数据
    salesTableBody.innerHTML = salesData.map(item => `
      <tr>
        <td>${item.name}</td>
        <td>${item.phone}</td>
        <td>¥${item.totalSales.toFixed(2)}</td>
      </tr>
    `).join('');

    // 移除加载指示器
    loadingIndicator.remove();
  } catch (error) {
    // 错误处理
    loadingIndicator.textContent = '数据加载失败，请稍后重试';
    loadingIndicator.style.color = 'red';
    console.error('数据加载失败:', error);
  }
});
